package file

import (
	"gin-project-01/gin-step03/gin12-cookie/controllers"
	"gin-project-01/gin-step03/gin12-cookie/models"
	"github.com/gin-gonic/gin"
	"log"
	"os"
	"path"
	"strconv"
)

type FileController struct {
	controllers.BaseController //继承 controllers.BaseController
}

// Upload
//
//	@Description: 上传单个文件
//	@receiver u
//	@param c
func (u FileController) Upload(c *gin.Context) {
	username := c.PostForm("username")
	log.Println("上传用户---------->>>>" + username)
	//获取到form表单中的文件
	file, err := c.FormFile("file")
	if err != nil {
		u.Error(c, "")
		return
	}
	//路径拼接、保存文件，如果没有文件夹会自行创建
	dist := path.Join("./static/upload", file.Filename)
	log.Println("文件名称---------->>>>" + dist)
	//save
	err = c.SaveUploadedFile(file, dist)
	if err != nil {
		u.Error(c, "保存文件失败")
	} else {
		u.Success(c, dist)
	}
}

// UploadBatch01
//
//	@Description: 多个文件上传，文件属性名称不一致
//	@receiver u
//	@param c
func (u FileController) UploadBatch01(c *gin.Context) {
	username := c.PostForm("username")
	log.Println("上传用户---------->>>>" + username)
	//文件一
	file1, _ := c.FormFile("file1")
	dist1 := path.Join("./static/upload", file1.Filename)
	log.Println("文件名称---------->>>>" + dist1)
	err1 := c.SaveUploadedFile(file1, dist1)
	//文件二
	file2, _ := c.FormFile("file2")
	dist2 := path.Join("./static/upload", file2.Filename)
	log.Println("文件名称---------->>>>" + dist2)
	err2 := c.SaveUploadedFile(file2, dist2)
	if err1 != nil || err2 != nil {
		u.Error(c, "")
	} else {
		u.Success(c, "./static/upload")
	}
}

// UploadBatch02
//
//	@Description: 多个文件上传，文件属性名称一致
//	@receiver u
//	@param c
func (u FileController) UploadBatch02(c *gin.Context) {
	username := c.PostForm("username")
	log.Println("上传用户---------->>>>" + username)
	//获取到form
	form, _ := c.MultipartForm()
	//获取到文件
	files := form.File["pic[]"]
	//保存文件路径
	var distList = make([]string, 0)
	for _, file := range files {
		dist := path.Join("./static/upload", file.Filename)
		err := c.SaveUploadedFile(file, dist)
		if err != nil {
			u.Error(c, "")
		} else {
			distList = append(distList, dist)
		}
	}
	u.Success(c, distList)
}

// UploadBatch03
//
//	@Description: 上传多个文件，文件属性名称一致，按照日期存储文件
//
// 1.创建文件保存目录  static/upload/20250106
// 2.获取到上传的文件
// 3.获取后缀名 判断类型是否正确  .jpg .png .jpeg
// 4.生成文件名和文件保存的目录
// 5.保存文件
//
//	@receiver u
//	@param c
func (u FileController) UploadBatch03(c *gin.Context) {
	var resList = make(map[string]interface{}, 2)
	var successList = make([]string, 0)
	var errorList = make([]string, 0)
	username := c.PostForm("username")
	log.Println("上传用户---------->>>>" + username)

	//1.创建文件保存目录  static/upload/20250106
	dir := path.Join("./static/upload/", models.GetDay())
	err := os.MkdirAll(dir, 0666) //MkdirAll 会创建多级目录
	if err != nil {
		u.Error(c, err.Error())
	}

	//获取到form
	form, _ := c.MultipartForm()
	//2.获取到上传的文件
	files := form.File["pic[]"]

	for _, file := range files {
		//3.获取后缀名 判断类型是否正确  .jpg .png .jpeg
		extName := path.Ext(file.Filename)
		allowMap := getAllowMap()
		exist, ok := allowMap[extName]
		if !exist || !ok {
			errorList = append(errorList, file.Filename+"文件类型错误")
			continue
		}
		//4.生成文件名和文件保存的目录 static/upload/20250106/1736143238290316700.png
		fileName := strconv.FormatInt(models.GetUnixNano(), 10) + extName
		dist := path.Join(dir, fileName)
		// 5.保存文件
		err := c.SaveUploadedFile(file, dist)
		if err != nil {
			u.Error(c, "")
		} else {
			successList = append(successList, dist)
		}
	}
	resList["success"] = successList
	resList["error"] = errorList
	u.Success(c, resList)
}

// getAllowMap
//
//	@Description:
//	@return map[string]bool
func getAllowMap() map[string]bool {
	var allowMap = make(map[string]bool)
	allowMap[".jpg"] = true
	allowMap[".png"] = true
	allowMap[".jpeg"] = true
	allowMap[".gif"] = true
	allowMap[".pdf"] = true
	allowMap[".webp"] = true
	return allowMap
}
